package com.huodongrili.calender;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huodongrili.calender.data.Constant;
import com.huodongrili.calender.model.Event;
import com.huodongrili.calender.util.HttpUtil;

public class EventDetailFragment extends Fragment implements OnClickListener{

	private Event mEvent;
	private TextView timeText;
	private TextView addrText;
	private TextView titleText;
	private TextView detailText;
	private LinearLayout placePane;
	private RelativeLayout placeLink;
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		Intent i = getActivity().getIntent();
		if(i!=null){
			mEvent= i.getParcelableExtra(Constant.EVENT_ENTRY);
		}
		
		if(mEvent!=null){
			timeText.setText(mEvent.getSTime());
			addrText.setText(mEvent.getAddr());
			titleText.setText(mEvent.getTitle());
			detailText.setText(mEvent.getContent());
		}else{
			placePane.setVisibility(View.GONE);
			placeLink.setVisibility(View.GONE);
		}
		
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_event_detail, container,
				false);
		placePane = (LinearLayout)v.findViewById(R.id.event_place);
		placeLink = (RelativeLayout)v.findViewById(R.id.event_link);
		
		timeText = (TextView)v.findViewById(R.id.event_time);
		addrText = (TextView)v.findViewById(R.id.event_addr);
		titleText = (TextView)v.findViewById(R.id.event_title);
		detailText = (TextView)v.findViewById(R.id.detail);
		
		placePane.setOnClickListener(this);
		placeLink.setOnClickListener(this);
		
		return v;
	}

	@Override
	public void onClick(View v) {
		
		if(mEvent==null)
			return;

		switch(v.getId()){
		case R.id.event_place:

			if(!HttpUtil.isNetworkAvailable(getActivity())){
				Toast.makeText(getActivity(), R.string.map_tip, Toast.LENGTH_SHORT).show();
				return;
			}
			
			Intent i = new Intent(getActivity(),LinkMapActivity.class);
			i.putExtra(Constant.EVENT_ADDR, mEvent.getAddr());
			startActivity(i);
			break;
		case R.id.event_link:
			
			if(!HttpUtil.isNetworkAvailable(getActivity())){
				Toast.makeText(getActivity(), R.string.link_tip, Toast.LENGTH_SHORT).show();
				return;
			}
			
			Intent ilink = new Intent(getActivity(),LinkWebActivity.class);
			ilink.putExtra(Constant.EVENT_LINK, mEvent.getLink());
			startActivity(ilink);
			break;
		}
		
	}

}
